﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RestaurantBooker.View;


namespace RestaurantBooker.Controls
{
    public partial class ResizeGrip : UserControl
    {
        public ResizeGrip()
        {
            InitializeComponent();
        }

        #region ChildWindowStyle

        private Point dialogGripMousePos;

        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            try
            {
                var root = App.Current.RootVisual as FrameworkElement;
                var cw = (sender as FrameworkElement).GetParent<ChildWindowContainer>();
                if (cw != null)
                {
                    var content = cw.Content as FrameworkElement;
                    if (content != null)
                    {
                        var newWidth = content.ActualWidth + e.HorizontalChange;
                        if (((!double.IsNaN(content.MinWidth) && newWidth >= content.MinWidth) && newWidth >= 100)
                            && ((!double.IsNaN(content.MaxWidth) && newWidth <= content.MaxWidth)
                            && dialogGripMousePos.X < root.ActualWidth - 10))
                            content.Width = newWidth;

                        var newHeight = content.ActualHeight + e.VerticalChange;
                        if (((!double.IsNaN(content.MinHeight) && newHeight >= content.MinHeight) && newHeight >= 100)
                            && ((!double.IsNaN(content.MaxHeight) && newHeight <= content.MaxHeight)
                            && dialogGripMousePos.Y < root.ActualHeight - 10))
                            content.Height = newHeight;
                    }
                }
            }
            catch { }
        }


        private void Thumb_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                dialogGripMousePos = e.GetPosition(App.Current.RootVisual as UIElement);
            }
            catch { }
        }

        #endregion              

    }
}
